<?php

namespace App\Http\Controllers\Admin;

use App\User;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use App\Model\AdminUser;

class AuthController extends Controller
{

    use AuthenticatesUsers;
    public function __construct()
     {

         //$this->middleware('guest.admin', ['except' => 'logout']);
     }
    protected function guard(){

        return auth()->$this->guard('admin');
    }
    public  function username()
    {

        return 'username';
    }

    public  function login(Request $request){



        if($request->isMethod('post')){

//            $message=[
//                'username.required'=>'用户名不能为空',
//                'username.min'=>'用户名位数不能小于5位',
//                'password.required'=>'密码不能为空',
//                 'password.min'=>'密码位数不能小于5位',
//            ];
//            $this->validate(request(),[
//                'username'=>'required|min:5',
//                'password'=>'required|min:5'
//            ],$message);

//            if(isset($_SESSION['errors'])){
//                $result['code']=0;
//                $result['msg']=isset($_SESSION['errors']['username'])?$_SESSION['errors']['username']:(isset($_SESSION['errors']['password'])?$_SESSION['errors']['password']:'登录失败');
//                return json_encode($result);
//            }
            //
            //print_r($errors->has('username'))
            $user=request(['username', 'password']);

            if(\Auth::guard('admin')->attempt($user)){
                $result['code']=200;
                $result['msg']='登录成功';
                return json_encode($result);
                //return redirect('/admin/index');
                Redirect::to('/admin/index')->send();
                //echo "是否已登录".Auth('admin')->check()."<br/>";die;
// 获取当前的认证用户
                //dd(Auth::guard('admin')->user());
// 获取当前的认证用户的 ID（未登录情况下会报错）
// 通过 Auth::attempt() 传入 true 值来开启 '记住我' 功能

// 只针对一次的请求来认证用户
                //Auth::once($credentials);
// 登录一个指定用户到应用上

// 登录指定用户 ID 的用户到应用上

//                Auth::guard('admin')->loginUsingId(2);
//                dd(Auth::guard('admin')->user());
//                die('成功 ');


            }else{
                $result['code']=0;
                $result['msg']='登录失败';
                return json_encode($result);
                die('失败');
            }
        }else{
            return view('home.login.index');
            //return view('admin.auth.login');
        }

    }
    //退出登录
    public function logout()
    {
        if(Auth::guard('admin')->user()){
            Auth::guard('admin')->logout();
        }
        return Redirect::to('admin/login');
    }
    //注册
//    public function register(Request $request)
//    {
//        if ($request->isMethod('post')) {
//            $validator = $this->validateRegister($request->input());
//            if ($validator->fails()) {
//                return back()->withErrors($validator)->withInput();
//            }
//            $user = new AdminUser();
//            $user->username = $request->username;
//            $user->password = bcrypt($request->password);
//            if($user->save()){
//                $user=request(['username', 'password']);
//                \Auth::guard('admin')->attempt($user);
//                Redirect::to('/admin/index');
//            }else{
//                return back()->with('error', '注册失败！')->withInput();
//            }
//        }
//        return view('admin.auth.register');
//    }
    protected function validateRegister(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|alpha_num|max:255',
            'account_number' => 'required|alpha_num|unique:admins',
            'password' => 'required|min:6|confirmed',
            'password_confirmation' => 'required|min:6|'
        ], [
            'required' => ':attribute 为必填项',
            'min' => ':attribute 长度不符合要求',
            'confirmed' => '两次输入的密码不一致',
            'unique' => '该账户已存在',
            'alpha_num' => ':attribute 必须为字母或数字',
            'max' => ':attribute 长度过长'
        ], [
            'name' => '昵称',
            'account_number' => '账号',
            'password' => '密码',
            'password_confirmation' => '确认密码'
        ]);
    }
}
